﻿using Bogus;
using Gridify.WebAPI.Data;
using Gridify.WebAPI.Models;
using Person = Gridify.WebAPI.Models.Person;

namespace Gridify.WebAPI.Extensions;

public static class FakeDataSeedingExtension
{
	public static void MakeSureDbCreated(this WebApplication app)
	{
		using var scope = app.Services.CreateScope();
		var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
        
		// 创建联系人数据的 Faker
		var contactFaker = new Faker<Contact>()
			.RuleFor(c => c.PhoneNumber, f => f.Phone.PhoneNumber("1##########"))
			.RuleFor(c => c.Address, f => f.Address.FullAddress());

		// 创建用户数据的 Faker
		var personFaker = new Faker<Person>()
			.RuleFor(u => u.UserName, f => f.Internet.UserName())
			.RuleFor(u => u.FirstName, f => f.Name.FirstName())
			.RuleFor(u => u.LastName, f => f.Name.LastName())
			.RuleFor(u => u.Age, f => f.Random.Int(18, 65))
			.RuleFor(u => u.Password, f => f.Internet.Password())
			.RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FirstName, u.LastName))
			.RuleFor(u => u.Contact, () => contactFaker.Generate());

		var fakeUsers = personFaker.Generate(20);

		db.Persons.AddRange(fakeUsers);
		db.SaveChanges();
	}
}
